const Router = require('@koa/router');
const { format } = require('mysql2');
// const jwt = require("jsonwebtoken")
const { OrderModel, UserModel } = require('../databases/index')
const router = new Router()
router.post("/deleteOrderById", async(ctx) => {
    const orderID = ctx.request.body;
    console.log(222, orderID.orderID);
    const orderid = await OrderModel.destroy({ where: { orderID: orderID.orderID } })
    if (orderid) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "ordersid success",
                orderid
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "订单不存在"
            }
        }
    }
})
router.post("/removeAllOrder", async(ctx) => {
    const {selectOrder} = ctx.request.body;
    const orderid = await OrderModel.destroy({ where: { orderID: selectOrder } })
    if (orderid) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "ordersid success",
                orderid
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "订单不存在"
            }
        }
    }
})
router.get("/getAllOrders", async(ctx) => {
    const order = await OrderModel.findAll();
    // console.log('订单详情', order)
    if (order) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "getAllOrder success",
                order
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "订单不存在"
            }
        }
    }
})
router.get('/getAllOrder', async function(ctx) {
    const { currentPage, pageSize } = ctx.request.query;
    console.log(currentPage, pageSize);
    offset = (currentPage - 1) * pageSize || 0; //偏移量的计算
    console.log(offset);
    let result = await OrderModel.findAndCountAll({ //findAndCountAll有两个参数：row，count
        offset,
        limit: parseInt(pageSize)
    })
    if (result) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "getAllOrder success",
                result
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "fail",
            data: {
                message: "订单不存在",
            }
        }
    }
});
router.post("/getOneOrder", async(ctx) => {
    const { orderID } = ctx.request.body;
    console.log(orderID);
    // const order = await OrderModel.findAll();
    const order = await OrderModel.findOne({ where: { orderID: orderID } });
    console.log('订单详情', order)
    if (order) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "updateOrder success",
                order
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "订单不存在"
            }
        }
    }
})
router.post("/updateOrder", async(ctx) => {
    let viewsData = ctx.request.body;
    console.log(6868, viewsData);
    const order = await OrderModel.update({
        address: viewsData.address, //更新
        orderstatus: viewsData.orderstatus //更新状态
    }, {
        where: {
            orderID: viewsData.orderID
        }
    });
    if (order.length > 0) {
        ctx.body = {
            code: 200,
            msg: "success",
        }
    } else {
        ctx.body = {
            code: 500,
            msg: "fail",
        }
    }
})
router.post("/searchOrder", async(ctx) => {
    const { Op } = require("sequelize");
    const { value } = ctx.request.body;
    const searchorders = await OrderModel.findAll({
        where: {
            [Op.or]: [{

                    orderID: {
                        [Op.like]: '%' + value + '%'
                    }
                },
                {
                    userID: {
                        [Op.like]: '%' + value + '%'
                    }
                }
            ]
        }
    })
    if (searchorders) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                searchorders
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/findOrders", async(ctx) => {
    const userID = ctx.request.body;
    // console.log(1111, userID);
    const orders = await OrderModel.findAll({ where: { userID: userID.userID } })
    console.log(1414, orders);
    if (orders) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                orders
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})

router.post("/addOrders", async(ctx) => {
    const  {addForm}  = ctx.request.body;
    console.log(addForm)
    const orders = await OrderModel.create({
        userID: addForm.userID,
        userName: addForm.userName,
        goodsimg:addForm.goodsimg,
        price: addForm.price,
        orderstatus: addForm.orderstatus,
        goodsID: addForm.goodsID,
        count: addForm.count,
        type: addForm.type,
        address: addForm.address,
        goodsname: addForm.goodsname,
    })
    console.log('牛逼', orders);
    if (orders) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                orders
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})

module.exports = router